package feifei.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/queryCookie.action")
public class QueryCookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("UTF-8");
       resp.setHeader("content-type","charset=UTF-8");
//       resp.setContentType("text/html,charset=UTF-8");
        Cookie[] cookies = req.getCookies();
        boolean isSave=false;
       String value="";
       if(cookies!=null){
           if(cookies.length!=0){
               for (Cookie cookie : cookies) {
                   if(cookie.getName().equals("username")){
                       //取出对应的值
                       value = cookie.getValue();
                       System.out.println("此处username的值为"+value);
                       isSave=true;
                       break;

                   }
               }

           }

       }
      if(isSave){
          resp.getWriter().write("存储在cookie中的username值为"+value);
      }
      else {
          resp.getWriter().write("存储在cookie中的username值为不存在");
      }

    }
}
